import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';
import * as session from 'express-session'
import { Request,Response,NextFunction } from 'express'
import { NestExpressApplication } from '@nestjs/platform-express'
import { join } from 'path';
import { TransformInterceptor } from './intercept/response'
import { HttpExceptionFilter } from './intercept/httpExceptionFilter'
import { ValidationPipe } from '@nestjs/common'
import { DocumentBuilder,SwaggerModule } from '@nestjs/swagger'

// function middlewareFunc(req:Request,res:Response,next:NextFunction){
//   console.log("全局中间件")
//   next()
// }

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  const corsOptions: CorsOptions = {
    origin: 'http://127.0.0.1:5173',
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
    preflightContinue: false,
    optionsSuccessStatus: 204,
    credentials: true,
  };
  // app.use(middlewareFunc)
  app.enableCors(corsOptions);
  app.useGlobalPipes(new ValidationPipe())
  app.useGlobalInterceptors(new TransformInterceptor())
  app.useStaticAssets(join(__dirname,'images'))
  app.useGlobalFilters(new HttpExceptionFilter())
  app.use(session({
    secret:'zgy',
    name:'zgy',
    rolling:true,
    cookie:{maxAge:999999},
    resave:false,
    saveUninitialized:false
  }))

  const options = new DocumentBuilder()
    .setTitle('xxxx')
    .setDescription('接口文档 sb yxk')
    .setVersion('1.0')
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api-docs', app, document);

  await app.listen(3000);
}
bootstrap();
